 /*
  * Mango CMS
  * Copyright 2008, Mango Web, and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
package org.mangocms.utils.generation;

import java.util.Random;

/**
 * Generates random keys/strings
 * 
 * @author <a href="tanordheim@gmail.com">Trond Arve Nordheim</a>
 * @version $Revision: 1.1 $
 */
public class RandomKeyGenerator {

	// A list of all letters available for use in a random key and/or string.
	// Any characters that can be misunderstood by a user (for instance "i" vs
	// "l") is not present.
	private static final String[] LETTERS = new String[] {
		"a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "m", "n", "o", "p",
		"q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D",
		"E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T",
		"U", "V", "W", "X", "Y", "Z"
	};
	
	// A list of all numbers available for use in a random key and//or string.
	// Any number that can be mistaken for a letter (for instance "0" vs "O")
	// is not present.
	private static final String[] NUMBERS = new String[] {
		"2", "3", "4", "5", "6", "7", "8", "9"
	};
	
	/// A combination of the LETTERS and NUMBERS arrays
	private static final String[] CHARS;
	
	// Static initializer - combines the LETTERS and NUMBERS array into the
	// CHARS array
	static {
		
		CHARS = new String[LETTERS.length + NUMBERS.length];
		System.arraycopy(LETTERS, 0, CHARS, 0, LETTERS.length);
		System.arraycopy(NUMBERS, 0, CHARS, LETTERS.length, NUMBERS.length);
		
	}
	
	/**
	 * Create a new RandomKeyGenerator.
	 * This constructor is private, preventing class instantiation. All public
	 * methods on this class are available as static methods.
	 */
	private RandomKeyGenerator() {
	}
	
	/**
	 * Get a random 10 character string consisting of random alphanumeric
	 * characters that can be used for hashing passwords and such.
	 * 
	 * @return
	 */
	public static String getEncryptionSalt() {
		
		StringBuilder salt = new StringBuilder();
		Random rand = new Random();
		for (int i = 0; i < 10; i++) {
			salt.append(CHARS[rand.nextInt(CHARS.length - 1)]);
		}
		return salt.toString();

	}

}
